package org.tsinghua.answer.utils;

import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.tsinghua.answer.domain.security.MyUserDetails;
import org.tsinghua.answer.domain.user.User;

public class AuthUtils {

	public static User getUser() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		MyUserDetails details = (MyUserDetails)authentication.getPrincipal();
		return details.getUser();
	}
	
	public static boolean isAuthenticated() {
		Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
		return authentication.isAuthenticated() 
				&& !(authentication instanceof AnonymousAuthenticationToken);
	}
}
